using System;
using JetBrains.Annotations;
using Kaleida.ServiceMonitor.Model.Parsing;
using log4net;

namespace Kaleida.ServiceMonitor.Model
{
    public static class ScriptSourceExtensions
    {
        private static readonly ILog log = LogManager.GetLogger(typeof (ScriptSourceExtensions));
        [NotNull]
        public static ScriptCompilation Compile([NotNull] this IScriptSource scriptSource)
        {
            if (scriptSource == null) throw new ArgumentNullException("scriptSource");
            try
            {
                var compiledScript = scriptSource.Content.ParseAndBuild(scriptSource.Name);
                return ScriptCompilation.Success(compiledScript);
            }
            catch (Exception exception)
            {
                return ScriptCompilation.Error(exception);
            }
        }

        public static string GetNameAndEditState(this IScriptSource script)
        {
            return script.Name + (script.IsDirty ? " (unsaved)" : "");
        }
    }
}